<template>
  <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" append-to-body v-dialogDrag width="70%">
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
               label-width="68px">
        <el-form-item label="费用名称" prop="name">
          <el-input v-model="queryParams.name" placeholder="请输入费用名称"
                    clearable @keyup.enter.native="handleQuery"/>
        </el-form-item>
        <el-form-item label="费用类型" prop="type">
          <el-select v-model="queryParams.type" placeholder="请选择费用类型" clearable>
            <el-option v-for="dict in dict.type.fee_type"
                       :key="dict.value" :label="dict.label" :value="dict.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="项目名称" prop="proName">
          <el-select v-model="queryParams.proId" placeholder="请选择项目名称" filterable
                     ref="proId">
            <el-option v-for="dict in proNameOptions"
                       :key="dict.value" :label="dict.label" :value="dict.value"/>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table border v-loading="loading" :data="dataList" @selection-change="handleSelectionChange" ref="table"
                :max-height="tableMaxHeight">
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column label="费用名称" align="center" prop="name"/>
        <el-table-column label="费用类型" align="center" prop="type">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.fee_type" :value="scope.row.type"/>
          </template>
        </el-table-column>
        <el-table-column label="金额" align="center" prop="money"/>
        <el-table-column label="项目名称" align="center" prop="proName"/>
        <el-table-column label="归属部门" align="center" prop="deptName"/>
        <el-table-column label="有无发票" align="center" prop="invoiceTax">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.reimburse_type" :value="scope.row.invoiceTax"/>
          </template>
        </el-table-column>
        <el-table-column label="备注" align="center" prop="remark"/>
        <!--<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-edit"
                       @click="handleUpdate(scope.row)" v-hasPermi="['develop:GoodsFee:edit']">修改
            </el-button>
            <el-popconfirm :title="`确认删除当前数据项？`" @confirm="handleDelete(scope.row)"
                           style="margin-left: 10px;">
              <el-button slot="reference" size="mini" type="text" icon="el-icon-delete"
                         v-hasPermi="['develop:GoodsFee:remove']">删除
              </el-button>
            </el-popconfirm>
          </template>
        </el-table-column>-->
      </el-table>

      <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum"
                  :limit.sync="queryParams.pageSize" @pagination="getList"/>
    </div>
    <div class="dialog-footer" slot="footer">
      <el-button @click="selectRowsBtn" type="primary">选 择</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
    import {deleteAction, getAction} from '@/api/manage';
    import GoodsFeeForm from '@/views/develop/goodsFee/module/GoodsFeeForm';
    import Treeselect from "@riophae/vue-treeselect";
    import "@riophae/vue-treeselect/dist/vue-treeselect.css";
    import {treeselect} from "@/api/system/dept";

    export default {
        name: "GoodsFeeListForSelect",
        components: {
            GoodsFeeForm,
            Treeselect,
        },
        dicts: ['fee_type'],
        data() {
            return {
                // 弹出层标题
                title: "选择报销商品",
                // 是否显示弹出层
                open: false,
                // 部门树选项
                deptOptions: [],
                // 遮罩层
                loading: true,
                // 选中数组
                ids: [],
                // 非单个禁用
                single: true,
                // 非多个禁用
                multiple: true,
                // 显示搜索条件
                showSearch: true,
                // 总条数
                total: 0,
                // 研发经费表格数据
                dataList: [],
                // 查询参数
                queryParams: {
                    pageNum: 1,
                    pageSize: 10,
                    name: null,
                    type: null,
                    yongtu: null,
                    proId: null,
                    proName: null,
                    reimburse: null,
                    teamId: null,
                    teamName: null,
                    invoice: null,
                    invoiceNo: null,
                    invoiceTax: null,
                    costPriceTax: null,
                    costTax: null,
                    imburseId: null,
                    params:{
                        dataFlag: "1"
                    },
                    createBy: this.$store.getters.name
                },
                url: {
                    list: '/goodsFee/goodsFee/list',
                    remove: '/goodsFee/goodsFee/',
                    export: '/goodsFee/goodsFee/export',
                    proNames: '/projectBaseInfo/projectBaseInfo/prjNames',
                },
                tableMaxHeight: window.innerHeight - 320,
                proNameOptions:[],
                selectRows: [],
            };
        },
        created() {
            this.getTreeselect();
            this.initProDicts();
            this.getList();
            window.addEventListener('resize', this.getTableHeight);
        },
        destroyed() {
            window.removeEventListener('resize', this.getTableHeight)
        },
        methods: {
            selectRowsBtn() {
                let that = this;
                that.$emit('ok', that.selectRows);
                that.open = false;
            },
            selectFeeListForImbursement(selectedIds){
                let that = this;
                that.open = true;
                that.queryParams.params.selectedIds = selectedIds;
                that.getList();
            },
            /** 查询部门下拉树结构 */
            getTreeselect() {
                treeselect().then(response => {
                    this.deptOptions = response.data;
                });
            },
            initProDicts() {
                let that = this;
                getAction(that.url.proNames, {orderByColumn: "create_time", isAsc: "desc"}).then(res => {
                    that.proNameOptions = res.data.nameList;
                })
            },
            /** 查询研发经费列表 */
            getList() {
                this.loading = true;
                getAction(this.url.list, this.queryParams).then(response => {
                    this.dataList = response.rows;
                    this.total = response.total;
                    this.loading = false;
                });
            },
            getTableHeight() {
                this.tableHeight = window.innerHeight - 320
            },
            /** 搜索按钮操作 */
            handleQuery() {
                this.queryParams.pageNum = 1;
                this.getList();
            },
            /** 重置按钮操作 */
            resetQuery() {
                this.resetForm("queryForm");
                this.handleQuery();
            },
            // 多选框选中数据
            handleSelectionChange(selection) {
                this.selectRows = selection;
                this.ids = selection.map(item => item.id)
                this.single = selection.length !== 1
                this.multiple = !selection.length
            },
            /** 新增按钮操作 */
            handleAdd() {
                this.$refs.form.handleAdd();
            },
            /** 修改按钮操作 */
            handleUpdate(row) {
                this.$refs.form.handleUpdate(row);
            },
            /** 删除按钮操作 */
            handleDelete(row) {
                const ids = row.id || this.ids;
                deleteAction(this.url.remove, ids).then(response => {
                    this.getList();
                    this.$modal.msgSuccess("删除成功");
                }).catch(() => {
                });
            },
            /** 导出按钮操作 */
            handleExport() {
                this.download(this.url.export, {
                    ...this.queryParams
                }, `goodsFee_${new Date().getTime()}.xlsx`)
            },
            // 取消按钮
            cancel() {
                this.open = false;
                this.selectRows = [];
            },
        }
    };
</script>

<style>
  .dept-tree-select .el-form-item--medium .el-form-item__content {
    height: 36px;
  }
</style>
